' PB_LiteBrowser.bas
#PBFORMS CREATED V2.01
'------------------------------------------------------------------------------
' The first line in this file is a PB/Forms metastatement.
' It should ALWAYS be the first line of the file. Other
' PB/Forms metastatements are placed at the beginning and
' end of "Named Blocks" of code that should be edited
' with PBForms only. Do not manually edit or delete these
' metastatements or PB/Forms will not be able to reread
' the file correctly.  See the PB/Forms documentation for
' more information.
' Named blocks begin like this:    #PBFORMS BEGIN ...
' Named blocks end like this:      #PBFORMS END ...
' Other PB/Forms metastatements such as:
'     #PBFORMS DECLARATIONS
' are used by PB/Forms to insert additional code.
' Feel free to make changes anywhere else in the file.
'------------------------------------------------------------------------------

#COMPILE EXE
#DIM ALL

'------------------------------------------------------------------------------
'   ** Includes **
'------------------------------------------------------------------------------
#PBFORMS BEGIN INCLUDES
#RESOURCE "PB_LiteBrowser.pbr"
%USEMACROS = 1
#INCLUDE ONCE "WIN32API.INC"
#INCLUDE ONCE "COMMCTRL.INC"
#INCLUDE ONCE "PBForms.INC"
#PBFORMS END INCLUDES
'------------------------------------------------------------------------------
#INCLUDE ONCE "Tooltips.inc"
#INCLUDE ONCE "PB_Common_Strings.inc"
#INCLUDE ONCE "PB_FileHandlingRoutines.inc"
#INCLUDE ONCE "PB_GDIplus_startup.inc"
#INCLUDE ONCE "PB_LoadJPG_as_Bitmap.inc"
'
GLOBAL g_lngTabcount AS LONG      ' total number of tabs
GLOBAL g_alngTabHandles() AS LONG ' array to hold tab handles
GLOBAL g_astrURLS() AS STRING     ' array to hold urls of tabs
GLOBAL g_astrURLhistory() AS STRING ' URL history of all tabs
GLOBAL g_lngLatestSlot() AS LONG    ' array to hold latest slot for each tab
'
TYPE udtImgTag      ' type for top right and bottom left coords
  lngTopX AS LONG   ' of an image
  lngTopY AS LONG
  lngBottomX AS LONG
  lngBottomY AS LONG
  strURL AS STRING * 1024
END TYPE
'
GLOBAL g_uURLs() AS udtImgTag       ' array for URL links
'
TYPE udtObject        ' type for start and end locations of objects
  lngStart AS POINT
  lngEnd AS POINT
END TYPE
'
GLOBAL g_strHTML   AS STRING  ' holds the HTML to be viewed
GLOBAL g_strLcHTML AS STRING  ' holds a lower case version of the HTML to be viewed
'
GLOBAL OldGraphicProc AS DWORD
'------------------------------------------------------------------------------
'   ** Constants **
'------------------------------------------------------------------------------
#PBFORMS BEGIN CONSTANTS
%IDD_dlgPBLiteBrowser =  101
%IDC_STATUSBAR1       = 1001
%IDC_TAB1             = 1002
%IDC_txtURL           = 1003
%IDC_IMGback          = 1004
%IDC_IMGForward       = 1005
%IDC_IMGReload        = 1006
%IDC_IMGHome          = 1007
%IDC_Graphic          = 1008
%IDD_dlgTabClick      =  102
%IDC_btnCloseTab      = 1009
#PBFORMS END CONSTANTS
'------------------------------------------------------------------------------
' redraw section
%IdCol           = 1
%WidthCol        = 2
%HeightCol       = 3
%MinWindowHeight = 250        ' minimum size of the height - window will
                              ' not shrink below this value
%MaxWindowHeight = 99999      ' maximum size of the window height
%MinWindowWidth  = 480        ' minimum size of the width - window will
                              ' not shrink below this value
%MaxWindowWidth  = 99999      ' maximum size of the width
'
%WidthMultiplier  = 3         ' used to set width of virtual graphic control
%HeightMultiplier = 3         ' used to set height of virtual graphic control
'
#INCLUDE ONCE "PB_Redraw.inc"
'
#RESOURCE MANIFEST 1,"XPTheme.xml"
#RESOURCE ICON 2000 "App.ico"
#RESOURCE ICON 2001 "BackButton.ico"
#RESOURCE ICON 2002 "NextButton.ico"
#RESOURCE ICON 2003 "SmallMagnify.ico"
'------------------------------------------------------------------------------
'   ** Declarations **
'------------------------------------------------------------------------------

#PBFORMS DECLARATIONS
'------------------------------------------------------------------------------
GLOBAL g_hFont AS DWORD        ' default font
GLOBAL g_hUFont AS DWORD       ' default underlined font
GLOBAL ga_hFonts() AS DWORD  ' array of fonts
GLOBAL ga_hUFonts() AS DWORD   ' array of underlined fonts
'
%BOLDFONT = 5                  ' element of ga_hFonts that is
                               ' 12 point bold
'
%MAX_TABS = 11                  ' maximum number of tabs allowed
%MAX_HISTORY = 10               ' maximum history on each tab
%MAX_URLS = 100                 ' maximum urls per page
GLOBAL g_lngTabSelected AS LONG ' number of currently selected tab
GLOBAL g_hDlg AS DWORD          ' global handle for main dialog
'
' print location
GLOBAL g_lngX, g_lngY AS LONG   ' Global X and Y print locations
'

'------------------------------------------------------------------------------
'   ** Main Application Entry Point **
'------------------------------------------------------------------------------
FUNCTION PBMAIN()
  PBFormsInitComCtls (%ICC_WIN95_CLASSES OR %ICC_DATE_CLASSES OR _
    %ICC_INTERNET_CLASSES)
    '
  funCreateFonts() ' create the fonts
  ' prepare the URLs arrays
  REDIM g_astrURLS(%MAX_TABS)       ' for current URL on each tab
  REDIM g_alngTabHandles(%MAX_TABS) ' for tab handles for each tab
  REDIM g_astrURLhistory(%MAX_TABS,%MAX_HISTORY) ' for history of all tabs
  REDIM g_lngLatestSlot(%MAX_TABS)  ' for latest slot for each tab
  REDIM g_uURLs(%MAX_URLS)          ' for URL links
  '
  ShowdlgPBLiteBrowser %HWND_DESKTOP
  ' tidy up fonts when app ending
  funUnloadFonts()
  '
END FUNCTION
'
FUNCTION funUnloadFonts() AS LONG
' unload all the fonts
  LOCAL lngF AS LONG
  '
  FONT END g_hFont
  FONT END g_hUFont
  ' end each of the fonts created
  FOR lngF = 1 TO 6
    FONT END ga_hFonts(lngF)
    FONT END ga_hUFonts(lngF)
  NEXT lngF
  '
END FUNCTION
'------------------------------------------------------------------------------
FUNCTION funCreateFonts() AS LONG
' create all the fonts needed
  REDIM ga_hFonts(6) AS DWORD  ' array for fonts
  REDIM ga_hUFonts(6) AS DWORD ' array for underlined fonts
  LOCAL lngF AS LONG
  LOCAL lngPoint AS LONG
  LOCAL lngStyle AS LONG
  LOCAL lngUnderlined AS LONG
  '
  lngPoint = 12
  ' create a new font
  FONT NEW "Courier New",lngPoint TO g_hFont
  ' create the underlined version
  FONT NEW "Courier New",lngPoint,5,0,0,0 TO g_hUFont
  '
  lngPoint = 30 ' set starting font size
  lngStyle = 1  ' set to bold
  lngUnderlined = 5 ' set to underlined (bold + underlined)
  '
  ' create each of the fonts
  FOR lngF = 1 TO 6
    FONT NEW "Courier New",lngPoint,lngStyle TO ga_hFonts(lngF)
    ' now create the underlined versions
    FONT NEW "Courier New",lngPoint,lngUnderlined TO ga_hUFonts(lngF)
    lngPoint = lngPoint - 4
  NEXT lngF
  '
END FUNCTION
'------------------------------------------------------------------------------
'   ** CallBacks **
'------------------------------------------------------------------------------
CALLBACK FUNCTION ShowdlgPBLiteBrowserProc()
  LOCAL lngPageDlgVar AS LONG    ' holds tab handle
  LOCAL lngControl AS LONG       ' handle of a control
  LOCAL strURL AS STRING         ' URL entered by user
  '
  LOCAL lngFileCount AS LONG     ' number of files being dropped
  LOCAL strFileName AS STRINGZ * %MAX_PATH   ' path/name of file
  LOCAL lngItem AS LONG          ' item number of dragged file
  '
  LOCAL ptnmhdr AS NMHDR PTR     ' information about a notification
  LOCAL lngPageNumber AS LONG    ' tab page number
  LOCAL lngTopPageNumber AS LONG ' maximum page number of tab
  LOCAL hMenuPopUp AS DWORD      ' handle for menu popup
  LOCAL strTabTitle AS STRING    ' the name on the selected tab
  LOCAL lngCurrentSlot AS LONG   ' currently selected history slot on the tab
  '
  SELECT CASE AS LONG CB.MSG
    CASE %WM_INITDIALOG
    ' Initialization handler
      STATIC hGraphic AS DWORD
      CONTROL HANDLE CB.HNDL, %IDC_GRAPHIC TO hGraphic
      OldGraphicProc = SetWindowLong(hGraphic, %GWL_WNDPROC, _
                       CODEPTR(GraphicProc))
      '
    ' insert a tab page
      TAB INSERT PAGE CB.HNDL, %IDC_TAB1, 1, 0, "Home" _
          TO lngPageDlgVar
          '
      ' store the tab handle
      g_alngTabHandles(1) = lngPageDlgVar
      '
      ' Add the + tab page
      TAB INSERT PAGE CB.HNDL, %IDC_TAB1, 2, 0, "+" _
          TO lngPageDlgVar
      g_alngTabHandles(2) = lngPageDlgVar
      '
      ' select Page 1
      g_lngTabSelected = 1
      TAB SELECT CB.HNDL, %IDC_TAB1, g_lngTabSelected
      '
      ' add the tooltips
      PREFIX "CALL ToolTip_SetToolTip (GetDlgItem(CB.HNDL, "
        %IDC_txtURL),"Enter URL or file path to HTML site/file", %YELLOW, %BLUE)
        %IDC_IMGback),"Click to go back to previous page", %YELLOW, %BLUE)
        %IDC_IMGForward),"Click to go forward to next page", %YELLOW, %BLUE)
        %IDC_IMGHome),"Click to go to home page", %YELLOW, %BLUE)
        %IDC_IMGReload),"Click to reload current page", %YELLOW, %BLUE)
      END PREFIX
      '
      ' disable navigation until html page loaded
      PREFIX "Control Disable cb.hndl,"
        %IDC_IMGback
        %IDC_IMGForward
      END PREFIX
      '
      ' set focus to the text URL control
      CONTROL SET FOCUS CB.HNDL,%IDC_txtURL
      '
    CASE %WM_DESTROY     ' <- Sent when the dialog is about to be destroyed
      IF OldGraphicProc THEN
        SetWindowLong hGraphic, %GWL_WNDPROC, OldGraphicProc
      END IF
      '
    CASE %WM_SIZE
    ' Dialog has been resized
      CONTROL SEND CB.HNDL, %IDC_STATUSBAR1, CB.MSG, CB.WPARAM, CB.LPARAM
      '
      IF ISTRUE isIconic(CB.HNDL) THEN EXIT FUNCTION  ' Exit if app is minimized
      '
      funResize CB.HNDL, 0, "Initialize"  ' Call this first
      ' now resize any controls
      funResizeControls(CB.HNDL)
      '
      funClearGraphicsControl()
      ' re-render the page content
      CONTROL GET TEXT CB.HNDL,%IDC_txtURL TO strURL
      funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
      '
    CASE %WM_NCACTIVATE
      STATIC hWndSaveFocus AS DWORD
      IF ISFALSE CB.WPARAM THEN
        ' Save control focus
        hWndSaveFocus = GetFocus()
      ELSEIF hWndSaveFocus THEN
        ' Restore control focus
        SetFocus(hWndSaveFocus)
        hWndSaveFocus = 0
      END IF
      '
    CASE %WM_DROPFILES
    ' handle objects being dragged to the dialog
      lngFileCount = DragQueryFile(CB.WPARAM, -1, BYVAL 0, 0)
      '
      IF lngFileCount = 1 THEN
      ' get the path & filename - support only one file
        lngItem = 0  ' first item is always zero
        DragQueryFile CB.WPARAM, lngItem, strFileName, SIZEOF(strFileName)
      END IF
      '
      ' tell windows drag and drop has completed
      DragFinish CB.WPARAM
      '
      strURL = strFileName
      '
      ' store the URL in global array
      TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
      g_astrURLS(lngPageNumber) = strURL
      '
      ' advance latest slot for this tab
      INCR g_lngLatestSlot(lngPageNumber)
      '
      ' store the new URL as latest web page
      funStoreURLHistory(lngPageNumber, strURL)
      '
      funClearGraphicsControl()
      '
      ' now update the graphics control with single file
      CONTROL SET TEXT CB.HNDL, %IDC_txtURL, strURL
      funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
      '
    CASE %WM_NOTIFY
    ' handle notifications
      ptnmhdr = CB.LPARAM
      SELECT CASE @ptnmhdr.idfrom
      '
       CASE %IDC_TAB1
        ' it's the tab control
          SELECT CASE @ptnmhdr.code
            '
            CASE %TCN_SELCHANGE
            ' tab has been changed
            ' get the tab handle
              TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
              ' store selected tab number
              g_lngTabSelected = lngPageNumber
              ' get the name on the tab
              TAB GET TEXT CB.HNDL, %IDC_TAB1, g_lngTabSelected TO strTabTitle
              '
              IF strTabTitle = "+" THEN
              ' last tab has been selected
                IF lngPageNumber + 1 =< %MAX_TABS THEN
                ' only if < max tabs allowed
                ' advance the top page number
                  lngTopPageNumber = lngPageNumber + 1
                  ' add a new tab
                  TAB SET TEXT CB.HNDL, %IDC_TAB1, lngPageNumber, "NEW"
                  '
                  TAB INSERT PAGE CB.HNDL, %IDC_TAB1, _
                                           lngTopPageNumber , 0, "+" _
                                           TO lngPageDlgVar
                  '
                  g_alngTabHandles(lngTopPageNumber) = lngPageDlgVar
                  ' a new tab so clear the graphics control
                  funClearGraphicsControl()
                  CONTROL SET TEXT CB.HNDL, %IDC_txtURL, ""
                  '
                  ' clear the tabs history
                  funClearHistoryForTab(lngPageNumber)
                  '
                ELSE
                ' set focus to previous tab
                  g_lngTabSelected = lngPageNumber - 1
                  TAB SELECT CB.HNDL, %IDC_TAB1, g_lngTabSelected
                '
                END IF
                '
              ELSE
              ' some other tab selected
              ' pick up the URL
                lngCurrentSlot = g_lngLatestSlot(lngPageNumber)
                strURL = g_astrURLhistory(lngPageNumber,lngCurrentSlot)
                ' reset buttons
                funSetBackForwardButtons(lngPageNumber,lngCurrentSlot)
                '
                ' populate the text box with URL
                CONTROL SET TEXT CB.HNDL, %IDC_txtURL, strURL
                ' clear and rerender the web page
                funClearGraphicsControl()
                funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
              '
              END IF
              '
          END SELECT
      END SELECT
    '
    CASE %WM_COMMAND
      ' Process control notifications
      SELECT CASE AS LONG CB.CTL
        ' /* Inserted by PB/Forms 08-15-2024 14:36:33
        CASE %IDC_STATUSBAR1
        '
        CASE %IDC_IMGback
          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
          ' go back a page
            ' get the tab handle
            TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
            lngCurrentSlot = g_lngLatestSlot(lngPageNumber)
            DECR lngCurrentSlot
            ' get the URL
            strURL = g_astrURLhistory(lngPageNumber,lngCurrentSlot)
            IF strURL <> "" THEN
            ' render the page
              ' populate the text box with URL
              CONTROL SET TEXT CB.HNDL, %IDC_txtURL, strURL
              '
              funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
              '
              ' store current slot
              g_lngLatestSlot(lngPageNumber) = lngCurrentSlot
              CONTROL ENABLE CB.HNDL,%IDC_IMGForward
              '
            END IF
            '
          END IF
          '
        CASE %IDC_IMGForward
          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
          ' go forward a page
            TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
            lngCurrentSlot = g_lngLatestSlot(lngPageNumber)
            INCR lngCurrentSlot
            ' get the URL
            strURL = g_astrURLhistory(lngPageNumber,lngCurrentSlot)
            '
            IF strURL <> "" THEN
            ' render the page
            ' populate the text box with URL
              CONTROL SET TEXT CB.HNDL, %IDC_txtURL, strURL
              '
              funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
              '
              ' store current slot
              g_lngLatestSlot(lngPageNumber) = lngCurrentSlot
              '
              ' set display of back/forward buttons
              funSetBackForwardButtons(lngPageNumber,lngCurrentSlot)
              '
            END IF
          '
          END IF
          '
        CASE %IDC_IMGHome
          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
            TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
            funDisplayHistory(lngPageNumber)
          END IF
        '
        CASE %IDC_IMGReload
          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
          ' reload the page
            CONTROL GET TEXT CB.HNDL,%IDC_txtURL TO strURL
            funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
          END IF
          '
        CASE %IDOK
        ' return has been pressed
          ' get the field that last had focus
        ' and return the dialog handle of that control
          lngControl = getDlgCtrlID(GetFocus())
          '
          IF lngControl = %IDC_txtURL THEN
          ' return has been pressed on the text URL field
            '
            funClearGraphicsControl()
            '
            CONTROL GET TEXT CB.HNDL,%IDC_txtURL TO strURL
            'funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
            '
            ' store the URL in global array
            TAB GET SELECT CB.HNDL, %IDC_TAB1 TO lngPageNumber
            g_astrURLS(lngPageNumber) = strURL
            '
            ' advance latest slot for this tab
            INCR g_lngLatestSlot(lngPageNumber)
            '
            ' store the new URL as latest web page
            funStoreURLHistory(lngPageNumber, strURL)
            '
            ' render the URL to the graphics control
            funRenderTheHTML(strURL, CB.HNDL, %IDC_TAB1)
            '
          END IF
          '
      END SELECT
  END SELECT
END FUNCTION
'
FUNCTION funProcess_a_URL(strURL AS STRING) AS LONG
' process a new URL into the current tab
  LOCAL lngPageNumber AS LONG
  LOCAL strPath AS STRING
  LOCAL strDelimeter AS STRING
  LOCAL lngEndElement AS LONG
  '
  ' first get the existing path
  CONTROL GET TEXT g_hDlg,%IDC_txtURL TO strPath
  '
  IF INSTR(strURL,ANY "/\") = 0 THEN
  ' this is relative path to wherever the existing URL is
    IF INSTR(strPath,"\") > 0 THEN
    ' determine delimiter
      strDelimeter = "\"
    ELSE
      strDelimeter = "/"
    END IF
    '
    ' if path is h:\html\index.html
    lngEndElement = PARSECOUNT(strPath,strDelimeter) -1
    ' lngEndElement will now be 2
    strPath = funStartRangeParse(strPath, _
                                 strDelimeter, _
                                 lngEndElement)
    ' and path will now be h:\html\
    ' now add the URL to the path
    strURL = strPath & strURL
  '
  ELSE
  ' new path?
  END IF
  '
  funClearGraphicsControl()
  '
  CONTROL SET TEXT g_hDlg,%IDC_txtURL, strURL
  '
  ' store the URL in global array
  TAB GET SELECT g_hDLG, %IDC_TAB1 TO lngPageNumber
  g_astrURLS(lngPageNumber) = strURL
  '
  ' advance latest slot for this tab
  INCR g_lngLatestSlot(lngPageNumber)
  '
  ' store the new URL as latest web page
  funStoreURLHistory(lngPageNumber, strURL)
  '
  ' render the URL to the graphics control
  funRenderTheHTML(strURL, g_hDLG, %IDC_TAB1)
  '
END FUNCTION
'
FUNCTION funSetBackForwardButtons(lngPageNumber AS LONG, _
                                  lngCurrentSlot AS LONG) AS LONG
' display/hide buttons based on page and slot
  IF g_astrURLhistory(lngPageNumber,lngCurrentSlot + 1) = "" THEN
  ' if next slot is blank - turn off forward button
    CONTROL DISABLE g_hDlg,%IDC_IMGForward
  ELSE
    CONTROL ENABLE g_hDlg,%IDC_IMGForward
  END IF
  '
  IF lngCurrentSlot = 1 THEN
  ' only one slot so turn of back button
    CONTROL DISABLE g_hDlg,%IDC_IMGBack
  ELSE
    CONTROL ENABLE g_hDlg,%IDC_IMGBack
  END IF
  '
END FUNCTION
'
FUNCTION funClearHistoryForTab(lngPageNumber AS LONG) AS LONG
' clear the history for the tab
  LOCAL lngR AS LONG
  '
  FOR lngR = 1 TO %MAX_HISTORY
    g_astrURLhistory(lngPageNumber, lngR) = ""
  NEXT lngR
  '
  ' set current slot to 0
  g_lngLatestSlot(lngPageNumber) = 0
  '
  PREFIX "CONTROL DISABLE g_hDlg,"
    %IDC_IMGback
    %IDC_IMGForward
  END PREFIX
'
END FUNCTION
'
FUNCTION funDisplayHistory(lngPageNumber AS LONG) AS LONG
' debug display tabs history
  LOCAL lngR AS LONG
  STATIC hWin AS DWORD
  '
  IF hWin = 0 THEN
    TXT.WINDOW("URL History",0,0) TO hWin
  ELSE
    TXT.CLS
  END IF
  '
  TXT.PRINT "Tab = ";lngPageNumber
  FOR lngR = 1 TO %MAX_HISTORY
    TXT.PRINT lngR;"  ";g_astrURLhistory(lngPageNumber, lngR)
  NEXT lngR
  '
END FUNCTION
'
FUNCTION funStoreURLHistory(lngPageNumber AS LONG, _
                            strURL AS STRING) AS LONG
' store the URL in the history
  LOCAL lngR AS LONG
  LOCAL lngCurrentSlot AS LONG
  '
  ' store in current slot
  lngCurrentSlot = g_lngLatestSlot(lngPageNumber)
  g_astrURLhistory(lngPageNumber,lngCurrentSlot) = strURL
  '
  ' blank all slots above this
  FOR lngR = lngCurrentSlot+1 TO %MAX_HISTORY
    g_astrURLhistory(lngPageNumber, lngR) = ""
  NEXT lngR
  ' set back forward buttons
  funSetBackForwardButtons(lngPageNumber,lngCurrentSlot)
  '
END FUNCTION
'
FUNCTION GraphicProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
                      BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
'--------------------------------------------------------------------
  ' Subclass procedure
  '------------------------------------------------------------------
  LOCAL lngX, lngY AS LONG
  LOCAL lngTabSelected AS LONG
  LOCAL strURL AS STRING
  LOCAL lngWidthVar, lngHeightVar AS LONG ' position of the graphic view
  '
  STATIC hHand AS DWORD         ' handle for the mouse cursor
  '
  SELECT CASE wMsg
    '
    CASE %WM_MOUSEMOVE
    ' mouse has moved
      lngX = LO(INTEGER, lParam)
      lngY = HI(INTEGER, lParam)
      '
      ' since we are using a virtual window
      ' get the begining of this window
      GRAPHIC GET VIEW TO lngWidthVar, lngHeightVar
      lngX = lngX + lngWidthVar
      lngY = lngY + lngHeightVar
      '
      ' determine if click is on a link area
      strURL = "" '
      IF ISTRUE funIsLink(lngX,lngY,strURL) THEN
      ' zone under cursor has hyperlink
      ' so set cursor
        IF hHand = 0 THEN
        ' cursor not yet loaded
          hHand = LoadCursor(%NULL, BYVAL %IDC_HAND)
        END IF
        SetCursor hHand
      END IF
    '
    CASE %WM_LBUTTONUP
      ' get mouse co-ords
      lngX = LO(INTEGER, lParam)
      lngY = HI(INTEGER, lParam)
      '
      ' since we are using a virtual window
      ' get the begining of this window
      GRAPHIC GET VIEW TO lngWidthVar, lngHeightVar
      lngX = lngX + lngWidthVar
      lngY = lngY + lngHeightVar

       ' determine if click is on a link area
      strURL = "" '
      IF ISTRUE funIsLink(lngX,lngY,strURL) THEN
      ' have we clicked on a link area?
      '  msgbox "YES"
      ' link to the new URL
        funProcess_a_URL(strURL)
      '
      END IF
    '
    CASE %WM_RBUTTONUP
    ' get mouse co-ords and show popup dialog
      lngX = LO(INTEGER, lParam)
      lngY = HI(INTEGER, lParam)
      '
      IF g_lngTabSelected > 1 THEN
      ' only for tabs other than first tab
        ShowdlgTabClick hWnd , lngX, lngY
      END IF
      '
  END SELECT
  '
  FUNCTION = CallWindowProc (OldGraphicProc, hWnd, wMsg, wParam, lParam)
  '
END FUNCTION
'
FUNCTION funIsLink(lngX AS LONG, _
                   lngY AS LONG, _
                   strURL AS STRING) AS LONG
' determine if the area clicked on is within
' a link area
'
  LOCAL lngR AS LONG
  ' check the uURLs array
  FOR lngR = 1 TO UBOUND(g_uURLs)
  ' sweep through all possible stored hyperlinks
    IF lngX > g_uURLs(lngR).lngTopX AND _
       lngX < g_uURLs(lngR).lngBottomX AND _
       lngY > g_uURLs(lngR).lngTopY AND _
       lngY < g_uURLs(lngR).lngBottomY THEN
    ' this is a link area
      strURL = TRIM$(g_uURLs(lngR).strURL)
      FUNCTION = %TRUE
      EXIT FUNCTION
    END IF
  NEXT lngR
  '
  FUNCTION = %FALSE
  '
END FUNCTION
'
FUNCTION funRenderTheHTML(strURL AS STRING, _
                          hDlg AS DWORD, _
                          lngTab AS LONG) AS LONG
' render the html to the graphics control
  LOCAL strHTML AS STRING         ' holds the html file
  LOCAL lngTabSelected AS LONG    ' tab number selected
  '
  ' exit immediately if no URL/PATH
  IF TRIM$(strURL) = "" THEN EXIT FUNCTION
  '
  GRAPHIC CLEAR %WHITE
  GRAPHIC COLOR %BLACK,%WHITE
  GRAPHIC SET FONT g_hFont ' set the default font
  GRAPHIC SET POS (1,1)
  '
  ' set the virtual page back to the start
  GRAPHIC SET VIEW 0,0
  GRAPHIC REDRAW
  '
  funClearGraphicsControl()
  '
  ' load the html file
  g_strHTML = funLoadHTMLPage(strURL)
  g_strLcHTML = LCASE$(g_strHTML)
  '
  IF g_strHTML <> "" THEN
  ' page has been loaded
    TAB GET SELECT hDlg, lngTab TO lngTabSelected
    TAB SET TEXT hDlg, lngTab, lngTabSelected, _
                       PARSE$(strURL,ANY "\/" ,-1)
  END IF
  '
  IF ISTRUE funValidateHTML() THEN
  ' is valid html
    'GRAPHIC PRINT "HTML formatting ok"
    funRenderHTMLTags(hDlg,lngTab)
    '
  ELSE
  ' html formatting error
    GRAPHIC ATTACH hDlg,%IDC_Graphic , REDRAW
    GRAPHIC PRINT "Invalid HTML formatting"
    'GRAPHIC PRINT g_strHTML
  END IF
  '
  GRAPHIC REDRAW
  '
END FUNCTION
'
FUNCTION funRenderHTMLTags(hDlg AS DWORD, _
                           lngTab AS LONG) AS LONG
' render each of the tags by stepping through each
' tag in the strHTML
  '
  LOCAL lngStart, lngEnd AS LONG
  LOCAL lngHTMLPos AS LONG
  LOCAL strChar AS STRING
  LOCAL strCommand AS STRING    ' current command
  LOCAL strLastCommand AS STRING ' last command executed
  LOCAL lngData AS STRING
  LOCAL strData AS STRING       ' data within a tag
  LOCAL lngInCommand AS LONG    ' true/false for in a command
  LOCAL lngExitCommand AS LONG
  LOCAL lngInBody AS LONG       ' true/false for inside body section
  LOCAL lngInHead AS LONG       ' true/false for inside head section
  LOCAL lngFont AS LONG         ' value of font selected
  LOCAL lngTabSelected AS LONG  ' currently selected tab
  LOCAL strTitle AS STRING      ' text for tab
  LOCAL lngAttribute AS LONG    ' start of attribute command
  LOCAL strAttributes AS STRING ' attributes of the command
  LOCAL lngRefTag AS LONG       ' true/false for inside a ref tag
  LOCAL uImgTag AS udtImgTag    ' type for link co-ords
  LOCAL strURL AS STRING        ' URL in a link
  LOCAL lngURL AS LONG          ' last populated URL
  LOCAL lngInParagraph AS LONG  ' true/false is inside a paragraph tag
  LOCAL lngImage AS LONG        ' true/false if <a> tag contains an image
  '
  ' as this is a new page reset all the click zones for this page
  RESET g_uURLs()
  '
  FOR lngHTMLPos = 1 TO LEN(g_strLcHTML)
  ' step through each character
    strChar = MID$(g_strLcHTML,lngHTMLPos,1)
    '
    IF strChar = "<" THEN
    ' start of a tag - pick up the command
      strLastCommand = strCommand  ' store last command
      '
      lngStart = lngHTMLPos + 1
      lngEnd = INSTR(lngStart,g_strLcHTML,">")
      strCommand = MID$(g_strLcHTML,lngStart,lngEnd-lngStart)
      '
       ' capture attributes
      ' e.g. strCommand = "p align=center"
      ' get position of first space
      lngAttribute = INSTR(strCommand," ")
      IF lngAttribute > 0 THEN
        strAttributes = TRIM$(MID$(strCommand,lngAttribute))
      ELSE
        strAttributes = ""
      END IF
      '
      ' trim command to remove attributes
      strCommand = PARSE$(strCommand," ",1)
      '
      lngInCommand = %TRUE
      lngExitCommand = %FALSE
      '
    ELSEIF strChar = ">" THEN
    ' end of a tag
      lngInCommand = %FALSE
      lngExitCommand = %TRUE
      '
      ' now tag has ended what do we do with the data
      SELECT CASE strCommand
        CASE "html","/html"
        ' do nothing
        CASE "body"
        ' inside the body section
          lngInBody = %TRUE
        CASE "/body"
        ' end of body tag
          lngInBody = %FALSE
        CASE "head"
        ' heading section
          lngInHead = %TRUE
          strTitle = ""  ' ensure any title is blank
        CASE "/head"
        ' end of heading section
          lngInHead = %FALSE
        CASE "h1","h2","h3","h4","h5","h6"
        ' heading start
          ' determine which heading number
          lngFont = VAL(RIGHT$(strCommand,1))
          GRAPHIC SET FONT ga_hFonts(lngFont)
          '
        CASE "/h1","/h2","/h3","/h4","/h5","/h6"
        ' end of heading
          GRAPHIC SET FONT g_hFont
          '
        CASE "title"
        ' start of the title
          IF ISTRUE lngInHead THEN
          ' pick up the title
            strTitle = funGetTagValue(lngStart, "</title>")
          END IF
          '
        CASE "/title"
        ' end of the title
          IF ISTRUE lngInHead THEN
          ' update the tab to show the name of the title
            TAB GET SELECT hDlg, lngTab TO lngTabSelected
            TAB SET TEXT hDlg, lngTab, lngTabSelected, strTitle
          END IF
          '
        CASE "p"
        ' paragraph start
          IF ISTRUE lngInBody THEN
          ' inside the body section
            lngInParagraph = %TRUE ' inside a paragraph
            '
            strData = funGetTagValue(lngStart, "</p>")
            'graphic print strData
            funPrintData(strData,lngRefTag,uImgTag)
          END IF
          '
        CASE "/p"
        ' end of paragraph
          IF ISTRUE lngInBody THEN
            lngInParagraph = %FALSE ' outside a paragraph
            IF strLastCommand = "/a" AND ISTRUE lngImage THEN
            ' last command was an <a> tag and contained an image
              g_lngX = 0
              g_lngY = g_lngY + uImgTag.lngBottomY - uImgTag.lngTopY
              GRAPHIC SET POS (g_lngX, g_lngY)
            '
              funPrintBlankLine()
            '
            ELSE
            ' handle the end of paragraphs for text
              funPrintBlankLine()
              funPrintBlankLine()
            END IF
            '
          END IF
          '
        CASE "img"
        ' start of image tag
          IF ISTRUE lngInBody THEN
          ' strAttributes should hold any attributes
            TAB GET SELECT hDlg, lngTab TO lngTabSelected
            '
            RESET uImgTag    ' blank out the UDT for tag
            '
            funDisplayImage(hDlg, strAttributes,lngTabSelected,_
                            uImgTag,lngInParagraph)
            '
            IF ISTRUE lngRefTag THEN
            ' we are inside a ref tag - so store the link
            ' strURL already populated
            ' advance the current URL
              INCR lngURL
              ' populate the link
              uImgTag.strURL = strURL
              ' save the udt into the global array
              g_uURLs(lngURL) = uImgTag
            '
            END IF
          '
          END IF
          '
        CASE "a"
        ' start of a ref tag
          lngRefTag = %TRUE
          strURL = TRIM$(PARSE$(strAttributes,"=",2),$DQ)
          ' get the data
          strData = funGetTagValue(lngStart, "</a>")
          '
          IF INSTR(strData,"=") = 0 THEN
          ' not an image source but plain text
            lngImage = %FALSE
            RESET uImgTag    ' blank out the UDT for tag
            ' we are inside a ref tag - so store the link
            ' strURL already populated
            ' advance the current URL
            INCR lngURL
            ' populate the link
            uImgTag.strURL = strURL
            '
            funPrintData(strData,lngRefTag,uImgTag)
            '
            ' save the udt into the global array
            g_uURLs(lngURL) = uImgTag
          '
          ELSE
          ' this is an image link
            lngImage = %TRUE
          END IF
        '
        CASE "/a"
        ' end of ref tag
          lngRefTag = %FALSE
          '
        CASE "u"
        ' underline
          GRAPHIC SET FONT g_hUFont
          ' get the data
          strData = funGetTagValue(lngStart, "</u>")
          funPrintData(strData,lngRefTag,uImgTag)
        '
        CASE "/u"
        ' end of underline
          GRAPHIC SET FONT g_hFont
          '
          IF ISTRUE lngInParagraph THEN
          ' inside a paragraph
            strData = funGetTagValue(lngStart, "</p>")
            funPrintData(strData,lngRefTag,uImgTag)
          END IF
          '
        CASE "table"
        ' handle tables
          funBuildTable(lngStart, strAttributes)
          '
        CASE ELSE
        ' handle anything else

      END SELECT
      '
    END IF
    '
  NEXT lngHTMLPos
  '
END FUNCTION
'
FUNCTION funBuildTable(lngStart AS LONG, _
                       strTableAttributes AS STRING) AS LONG
' build a table structure
  LOCAL strData AS STRING     ' characters in the table
  LOCAL strLcData AS STRING   ' lower case characters in the table
  LOCAL lngT AS LONG              ' character position
  LOCAL strChar AS STRING         ' character being processed
  LOCAL strLastCommand AS STRING  ' last command
  LOCAL strCommand AS STRING      ' current command
  LOCAL lngTStart AS LONG         ' start of command
  LOCAL lngTEnd AS LONG           ' end of command
  LOCAL lngAttribute AS LONG      ' attribute flag
  LOCAL strAttributes AS STRING   ' attribute text
  LOCAL lngInCommand AS LONG      ' in command flag
  LOCAL lngExitCommand AS LONG    ' exit command flag
  '
  LOCAL lngHeaderColumns AS LONG  ' no. of header columns
  LOCAL lngDataColumns AS LONG    ' no. of data columns
  LOCAL lngRowCount AS LONG       ' rows in the array
  LOCAL lngColumnCount AS LONG    ' columns in the array
  LOCAL lngRow AS LONG            ' array row number
  LOCAL lngColumn AS LONG         ' array column number
  '
  LOCAL strColumnData AS STRING   ' data for column
  LOCAL strCaption AS STRING      ' caption for the table
  '
  LOCAL lngHeader AS LONG         ' true/false for header in table
  '
  '
  ' get all the table data
  strData = funGetTagValue(lngStart, "</table>")
  strLcData = LCASE$(strData)
  '
  lngHeaderColumns = TALLY(strLcData,"<th>")
  lngDataColumns   = TALLY(strLcData,"<td>")
  lngRowCount = TALLY(strLcData,"<tr>")
  '

  ' get the largest column count
  lngColumnCount = MAX(lngHeaderColumns,lngDataColumns)
  '
  IF lngHeaderColumns > 0 AND lngRowCount > 2 THEN
  ' handle multiple rows
    lngColumnCount = lngDataColumns/(lngRowCount-1)
  END IF
  '
  ' prep the table array to hold data
  DIM a_strTable(1 TO lngRowCount,1 TO lngColumnCount) AS STRING

  '
  FOR lngT = 1 TO LEN(strData)
    strChar = MID$(strLcData,lngT,1)
    '
    IF strChar = "<" THEN
    ' start of a tag - pick up the command
      strLastCommand = strCommand  ' store last command
      '
      lngTStart = lngT + 1
      lngTEnd = INSTR(lngTStart,strData,">")
      strCommand = MID$(strLcData,lngTStart,lngTEnd-lngTStart)
      '
      ' capture attributes
      ' e.g. strCommand = "tr align=center"
      ' get position of first space
      lngAttribute = INSTR(strCommand," ")
      IF lngAttribute > 0 THEN
        strAttributes = TRIM$(MID$(strCommand,lngAttribute))
      ELSE
        strAttributes = ""
      END IF
      '
      ' trim command to remove attributes
      strCommand = PARSE$(strCommand," ",1)
      '
      lngInCommand = %TRUE
      lngExitCommand = %FALSE
      '
    ELSEIF strChar = ">" THEN
    ' end of a tag
      lngInCommand = %FALSE
      lngExitCommand = %TRUE
      '
      ' now tag has ended what do we do with the data
      SELECT CASE strCommand
        CASE "caption"
        ' caption of the table - store the value
          strCaption = funGetTableTagValue(lngT, "</caption>", _
                                           strData,strLcData)
        '
        CASE "tr"
        ' new row
          INCR lngRow
          '
        CASE "/tr"
        ' end of row
          lngColumn = 0
        '
        CASE "th"
        ' start of header column
          INCR lngColumn
          ' get the data
          strColumnData = funGetTableTagValue(lngT, "</th>", _
                                         strData,strLcData)
          ' store the data in the array
          a_strTable(lngRow,lngColumn) = strColumnData
          '
          lngHeader = %TRUE
          '
        CASE "/th"
        ' end of header column
        CASE "td"
        ' start of data column
          INCR lngColumn
          ' get the data
          strColumnData = funGetTableTagValue(lngT, "</td>", _
                                         strData,strLcData)
          ' store the data in the array
          a_strTable(lngRow,lngColumn) = strColumnData
          '
        CASE "/td"
        ' end of data column
        '
      END SELECT
    END IF
    '
  NEXT lngT
  '
  ' reached the end of the table details
  ' end of table - so display it on graphics control
  funDisplayTable(a_strTable(), strCaption, _
                  lngHeader, LCASE$(strTableAttributes))
  '
END FUNCTION
'
FUNCTION funDisplayTable(BYREF a_strTable() AS STRING, _
                         strCaption AS STRING, _
                         lngHeader AS LONG, _
                         strTableAttributes AS STRING) AS LONG
' display the table on the graphics control
  '
  LOCAL lng_imgW, lng_imgH AS LONG  ' width and height of one character
  LOCAL lngCaptionWidth AS LONG     ' width of the caption
  LOCAL lngTableWidth AS LONG       ' width of the whole table
  LOCAL lngColumns AS LONG          ' number of columns in table
  LOCAL lngCaptionOffset AS LONG    ' Offset used to position Caption
  '
  LOCAL lngColumn AS LONG           ' column counter
  LOCAL lngRow AS LONG              ' row counter
  LOCAL lngRows AS LONG             ' total rows
  '
  LOCAL lngTableBorder AS LONG      ' true/false for table having a border
  LOCAL uTable AS udtObject         ' used for start and end locations of table
  LOCAL uTempPosition AS udtObject  ' used for temp storage of position
  LOCAL uVerticalLines AS udtObject ' used for tabels vertical lines
  '
  LOCAL strTableWidthAmount AS STRING ' % or pixel size of table
  '
  LOCAL lngXOffset AS LONG            ' offset for each column
  '
  IF INSTR(strTableAttributes,"border=1")> 0 THEN
  ' table is to have border
    lngTableBorder = %TRUE
  END IF
  '
  IF INSTR(strTableAttributes,"width=")> 0 THEN
  ' table has width setting
    strTableWidthAmount = PARSE$(strTableAttributes,"width=",2)
    strTableWidthAmount = PARSE$(strTableWidthAmount," ",1)
  '
  END IF
  '
  ' get the number of columns in the array
  lngColumns = UBOUND(a_strTable(),2)
  '
  DIM a_lngTableColumns(1 TO lngColumns) AS LONG
  '
  ' get the size of one character
  GRAPHIC CELL SIZE TO lng_imgW, lng_imgH
  ' get the width of the table and each column in array
  lngTableWidth = funWidthOfTable(a_strTable(),lng_imgW, _
                                  lng_imgH,a_lngTableColumns(), _
                                  strTableWidthAmount, _
                                  lngHeader)
  '
  IF strCaption <> "" THEN
  ' there is a caption
  ' calculate the width of all characters in the caption
    lngCaptionWidth = LEN(strCaption) * lng_imgW
    '
    IF lngCaptionWidth < lngTableWidth THEN
    ' caption is smaller width than table
      lngCaptionOffset = (lngTableWidth - lngCaptionWidth) \ 2
      ' set the starting print position
      g_lngX = g_lngX + lngCaptionOffset
    END IF
    '
    ' print the caption
    GRAPHIC SET POS (g_lngX, g_lngY)
    GRAPHIC PRINT strCaption
    GRAPHIC GET POS TO g_lngX, g_lngY
    '
  END IF
  '
  ' now we know the width of each column we can print
  ' the data to the graphics control
  GRAPHIC GET POS TO g_lngX, g_lngY
  '
  ' store starting position
  PREFIX "uTable."
    lngStart.X = g_lngX + (lng_imgW\3)
    lngStart.Y = g_lngY
  END PREFIX
  '
  lngRows = UBOUND(a_strTable,1)
  '
  FOR lngRow = 1 TO lngRows
    '
    IF lngRow = 1 AND lngHeader = %TRUE THEN
    ' first row header should be bold
      GRAPHIC SET FONT ga_hFonts(%BOLDFONT)
    END IF
    '
    FOR lngColumn = 1 TO UBOUND(a_strTable,2)
    ' print the column data
      GRAPHIC SET POS (g_lngX, g_lngY)
      '
      IF lngColumn = 1 THEN
      ' print padding before first column
        ' add on a one character padding
        g_lngX = g_lngX + lng_imgW
        GRAPHIC SET POS (g_lngX, g_lngY)
      ELSE
      ' for remainder of columns
      ' add on width of previous column
        g_lngX = funGetPreviousColumnsWidth(a_lngTableColumns(),lngColumn)
        GRAPHIC SET POS (g_lngX, g_lngY)
        '
      END IF
      '
      GRAPHIC PRINT a_strTable(lngRow,lngColumn);
      '
    NEXT lngColumn
    '
    IF lngRow = 1 AND lngHeader = %TRUE THEN
    ' reset the font to normal
      GRAPHIC SET FONT g_hFont
    END IF
    '
    GRAPHIC PRINT ""
    GRAPHIC GET POS TO g_lngX, g_lngY
    ' move half a character line downwards
    g_lngY = g_lngY + (lng_imgH\2)
    '
    IF ISTRUE lngTableBorder  AND lngRow <> lngRows THEN
    ' draw horizontal line - except for last row
      GRAPHIC LINE (uTable.lngStart.X,g_lngY - (lng_imgH\3) ) - _
                   (lngTableWidth,g_lngY - (lng_imgH\3)), _
                   %BLACK
    END IF
    '
  NEXT lngRow
  '
  'GRAPHIC GET POS TO g_lngX, g_lngY
  '
   ' store ending position
  PREFIX "uTable."
    lngEnd.X = lngTableWidth
    lngEnd.Y = g_lngY - (lng_imgH\3)
  END PREFIX
  '
  IF ISTRUE lngTableBorder THEN
    GRAPHIC BOX (uTable.lngStart.X  ,uTable.lngStart.Y) - _
                (uTable.lngEnd.X,uTable.lngEnd.Y),0,%BLACK
    '
    FOR lngColumn = 1 TO lngColumns -1
    ' draw vertical lines
      PREFIX "uVerticalLines."
        lngStart.X = lngXOffset + uTable.lngStart.X + a_lngTableColumns(lngColumn) - lng_imgW
        lngStart.Y = uTable.lngStart.Y
        lngEnd.X   = uVerticalLines.lngStart.X
        lngEnd.Y   = uTable.lngEnd.Y
      END PREFIX
      ' store offset for next column
      lngXOffset = uVerticalLines.lngStart.X
      '
      GRAPHIC LINE (uVerticalLines.lngStart.X,uVerticalLines.lngStart.Y) - _
                   (uVerticalLines.lngEnd.X,uVerticalLines.lngEnd.Y),%BLACK
      '
    NEXT lngColumn
    '
  END IF
  '
END FUNCTION
'
FUNCTION funGetPreviousColumnsWidth(BYREF a_lngTableColumns() AS LONG, _
                                    lngColumn AS LONG) AS LONG
' return the widths of the previous column
  LOCAL lngPColumn AS LONG    ' previous columns
  LOCAL lngWidth AS LONG      ' Cumulative width
  '
  FOR lngPColumn = 1 TO lngColumn - 1
    lngWidth = lngWidth + a_lngTableColumns(lngPColumn)
  NEXT lngPColumn
  '
  FUNCTION = lngWidth
  '
END FUNCTION
'
FUNCTION funGetControlSize() AS LONG
' return the width of the graphics control
  FUNCTION = GRAPHIC(SIZE.X)
'
END FUNCTION
'
FUNCTION funWidthOfTable(BYREF a_strTable() AS STRING, _
                         lng_imgW AS LONG, _
                         lng_imgH AS LONG, _
                         BYREF a_lngTableColumns() AS LONG, _
                         strTableWidthAmount AS STRING, _
                         lngHeader AS LONG) AS LONG
' work out width of a table for each column
'
  LOCAL lngColumn AS LONG           ' column counter
  LOCAL lngColumns AS LONG          ' total number of columns
  LOCAL lngRow AS LONG              ' row counter
  LOCAL lngRows AS LONG             ' total number of rows
  LOCAL lngTotalWidth AS LONG       ' total width of the table
  LOCAL lngScaled AS LONG           ' table is scaled i.e. width preset
  '
  lngColumns = UBOUND(a_lngTableColumns)
  lngRows = UBOUND(a_strTable,1)
  '
  IF strTableWidthAmount <> "" THEN
  ' table is set for fixed width
    lngScaled = %TRUE
    IF RIGHT$(strTableWidthAmount,1) = "%" THEN
      lngTotalWidth = funGetControlSize() * _
                      (VAL(strTableWidthAmount)/100)
    ELSE
    ' width set in pixels
      lngTotalWidth = VAL(strTableWidthAmount)
    END IF
    '
  ELSE
    lngScaled = %FALSE
  END IF
  '
  FOR lngColumn = 1 TO lngColumns
  ' look through each column
    IF ISTRUE lngScaled THEN
    ' this table is scaled in width
      a_lngTableColumns(lngColumn) = lngTotalWidth \ lngColumns
      '
    ELSE
    ' this table is dynamic
    FOR lngRow = 1 TO lngRows
    ' and get max length of text in each row
    ' of that column
       IF lngRow = 1 AND ISTRUE lngHeader THEN
       ' its the first row and header is present
         a_strTable(lngRow,lngColumn) = _
         a_strTable(lngRow,lngColumn) & SPACE$(2)
       '
       ELSE
         a_lngTableColumns(lngColumn) = _
                           MAX(LEN(a_strTable(lngRow,lngColumn)), _
                           LEN(a_strTable(lngRow -1,lngColumn)))
       ' store width plus 2 padding characters
         a_lngTableColumns(lngColumn) = _
                          (a_lngTableColumns(lngColumn) * lng_imgW) + _
                          (2 * lng_imgW)
       END IF
    NEXT lngRow
    END IF
    '
  NEXT lngColumn
  '
  IF ISFALSE lngScaled THEN
  ' return the total width of the table
    FOR lngColumn = 1 TO UBOUND(a_lngTableColumns)
      lngTotalWidth = lngTotalWidth + a_lngTableColumns(lngColumn)
    NEXT lngColumn
    '
    ' add on a gap between first and last columns
    lngTotalWidth = lngTotalWidth + (2 * lng_imgW)
  '
  END IF
  '
  FUNCTION = lngTotalWidth
  '
END FUNCTION
'
FUNCTION funDisplayImage(hDlg AS DWORD, _
                         strAttributes AS STRING, _
                         lngTabSelected AS LONG, _
                         uImgTag AS udtImgTag, _
                         lngInParagraph AS LONG) AS LONG
' display the image on the graphics control
  LOCAL lngStartPosition AS LONG
  LOCAL strTemp AS STRING
  LOCAL strSource AS STRING
  LOCAL lng_imgW, lng_imgH AS LONG ' width and height of returned bitmap
  LOCAL hBMP AS DWORD              ' handle of the bitmap
  LOCAL strFullHTMLPath AS STRING  ' full path to the html
  LOCAL strPath AS STRING
  LOCAL lngEndElement AS LONG
  LOCAL lngX, lngY AS LONG         ' current position of cursor
  '
  lngStartPosition = INSTR(strAttributes,"src=") + LEN("src=")
  strTemp = MID$(strAttributes,lngStartPosition)
  strSource = PARSE$(strTemp,$DQ,2)
  '
  ' image path may be relative to html page
  strFullHTMLPath = g_astrURLS(lngTabSelected)
  lngEndElement = PARSECOUNT(strFullHTMLPath,"\")
  DECR lngEndElement
  strPath = funStartRangeParse(strFullHTMLPath,"\",lngEndElement)
  '
  IF ISTRUE funLoadImageFile(strPath & strSource, _
                             lng_imgW, _
                             lng_imgH, _
                             hBMP ) THEN
  ' reattach to graphics control
    GRAPHIC ATTACH hDlg, %IDC_Graphic
    ' copy bitmap
    GRAPHIC COPY hBmp,0 TO (g_lngX,g_lngY)
    '
    ' store the location
    PREFIX "uImgTag."
      lngTopX = g_lngX
      lngTopY = g_lngY
      lngBottomX = g_lngX + lng_imgW
      lngBottomY = g_lngY + lng_imgH
    END PREFIX
    '
    ' now update global position
    '
    IF ISTRUE lngInParagraph THEN
    ' we are inside a paragraph tag
      g_lngX = g_lngX + lng_imgW
    ELSE
    ' outside a paragraph tag
      g_lngX = 0
      g_lngY = g_lngY + lng_imgH
    END IF
    '
    GRAPHIC BITMAP END
    ' redraw for user
    GRAPHIC REDRAW
    '
  END IF
END FUNCTION
'
FUNCTION funGetTagValue(lngStartPosition AS LONG, _
                        strTagEnd AS STRING) AS STRING
' return the content of the tag
  LOCAL lngEnd AS LONG
  '
  lngStartPosition = INSTR(lngStartPosition,g_strLcHTML,">") +1
  lngEnd   = INSTR(lngStartPosition,g_strLcHTML,strTagEnd)
  FUNCTION = MID$(g_strHTML,lngStartPosition,lngEnd-lngStartPosition)
'
END FUNCTION
'
FUNCTION funGetTableTagValue(lngStartPosition AS LONG, _
                             strTagEnd AS STRING, _
                             strData AS STRING, _
                             strLcData AS STRING) AS STRING
' return the content of the Table tag
  LOCAL lngEnd AS LONG
  '
  lngStartPosition = INSTR(lngStartPosition,strData,">") +1
  lngEnd   = INSTR(lngStartPosition,strLcData,strTagEnd)
  FUNCTION = MID$(strData,lngStartPosition,lngEnd-lngStartPosition)
  '
END FUNCTION
'
FUNCTION funPrintData(strData AS STRING, _
                      lngRefTag AS LONG, _
                      uImgTag AS udtImgTag) AS LONG
' print the data string to the graphics control
  LOCAL lngPos AS LONG
  LOCAL strTempData AS STRING
  LOCAL lngBreaks AS LONG      ' number of line breaks
  LOCAL lngB AS LONG
  LOCAL strText AS STRING      ' string to print
  '
  LOCAL lng_imgW, lng_imgH AS LONG ' width and height of
  '                                  the hyperlink
  strTempData = LCASE$(strData)
  '
  IF ISTRUE lngRefTag AND TRIM$(strData) <> "" THEN
  ' work out the height and width of the
  ' text for one character
    GRAPHIC CELL SIZE TO lng_imgW, lng_imgH
    ' calculate the width of all characters
    lng_imgW = LEN(strData) * lng_imgW
    ' add a little to height
    lng_imgH = lng_imgH +4
    '
  ' store the location
    PREFIX "uImgTag."
      lngTopX = g_lngX
      lngTopY = g_lngY
      lngBottomX = g_lngX + lng_imgW
      lngBottomY = g_lngY + lng_imgH
    END PREFIX
    ' set the font to be underlined
    GRAPHIC COLOR %BLUE,%WHITE
    GRAPHIC SET FONT g_hUFont
    '
  END IF
  '
  ' test for embedded link?
  IF INSTR(strTempData,"a href=") > 0 THEN
  ' embedded image or hyperlink
   '
    EXIT FUNCTION
    '
  ELSEIF INSTR(strTempData,"<u>") > 0 THEN
  ' embedded <u> tag
    '
    EXIT FUNCTION
  END IF
  '
  lngBreaks = PARSECOUNT(strTempData,"<br>")
  '
  IF lngBreaks > 1 THEN
  ' we have one or more embedded line breaks
    FOR lngB = 1 TO lngBreaks
    ' get the data
      strText = TRIM$(PARSE$(strData,"<br>",lngB))
      IF TRIM$(strData) <> "" THEN
      ' print if text is not blank
        GRAPHIC SET POS (g_lngX, g_lngY)
        GRAPHIC PRINT strText
        GRAPHIC GET POS TO g_lngX, g_lngY
      END IF
      '
    NEXT lngB
    '
  ELSE
  ' no line breaks
    GRAPHIC SET POS (g_lngX, g_lngY)
    GRAPHIC PRINT strData;
    GRAPHIC GET POS TO g_lngX, g_lngY
  '
    IF ISTRUE lngRefTag THEN
    ' reset default font and colour
      GRAPHIC COLOR %BLACK,%WHITE
      GRAPHIC SET FONT g_hFont
    END IF
    '
  END IF
  '
END FUNCTION
'
FUNCTION funPrintBlankLine() AS LONG
' print a blank line on the graphics control
  GRAPHIC SET POS (g_lngX, g_lngY)
  GRAPHIC PRINT ""
  GRAPHIC GET POS TO g_lngX, g_lngY
END FUNCTION
'
FUNCTION funValidateHTML() AS LONG
' validate the html
  '
  IF INSTR(g_strLcHTML,"<html>") = 0 THEN
    FUNCTION = %FALSE:EXIT FUNCTION
    IF INSTR(g_strLcHTML,"</html>") = 0 THEN
      FUNCTION = %FALSE:EXIT FUNCTION
    ELSE
      IF INSTR(g_strLcHTML,"<body>") = 0 THEN
        FUNCTION = %FALSE:EXIT FUNCTION
      ELSE
        IF INSTR(g_strLcHTML,"</body>") = 0 THEN
          FUNCTION = %FALSE:EXIT FUNCTION
        END IF
      END IF
    END IF
  END IF
  '
  FUNCTION = %TRUE
  '
END FUNCTION
'
FUNCTION funLoadHTMLPage(strURL AS STRING) AS STRING
' determine if file or web page and return the content
' of the file
  LOCAL strHTML AS STRING
  ' read in the html data trimming out leading
  ' and trailing spaces
  strHTML = TRIM$(funBinaryFileAsString(strURL))
  ' remove any CR/LF
  strHTML = REMOVE$(strHTML, ANY $CRLF)
  '
  FUNCTION = strHTML
  '
END FUNCTION
'
FUNCTION funClearGraphicsControl() AS LONG
' repaint the graphics control
  ' reset the co-ords
  g_lngX = 0 : g_lngY = 0
  GRAPHIC CLEAR %RGB_WHITE ,0
  GRAPHIC REDRAW
  '
END FUNCTION
'------------------------------------------------------------------------------
FUNCTION funResizeControls(hDlg AS DWORD) AS LONG
' resize the windows controls
'
  LOCAL lngWide, lngHigh AS LONG
' return to fixed size to avoid resize issues
  GRAPHIC SET FIXED
  '
  funResize hDlg, %IDC_txtURL, "Scale-H"
  funResize hDlg, %IDC_TAB1,   "Scale-H"
  funResize hDlg, %IDC_Graphic,"Scale-H"
  funResize hDlg, %IDC_Graphic,"Scale-V"
  '
  ' repaint the form
  funResize hDlg, 0, "Repaint"
  '
  ' repaint the graphics control
  GRAPHIC CLEAR %RGB_WHITE ,0
  ' get the current size of the graphics window
  GRAPHIC GET SIZE TO lngWide, lngHigh
  '
  ' set the size of a virtual graphics control
  GRAPHIC SET VIRTUAL  (lngWide * %WidthMultiplier)-5, _
                        lngHigh * %HeightMultiplier
  '
  GRAPHIC REDRAW
  '
END FUNCTION

'------------------------------------------------------------------------------
'   ** Dialogs **
'------------------------------------------------------------------------------
FUNCTION ShowdlgPBLiteBrowser(BYVAL hParent AS DWORD) AS LONG
  LOCAL lngResult AS LONG
  LOCAL lngWide AS LONG     ' width of the graphics control
  LOCAL lngHigh AS LONG     ' height of the graphics control

#PBFORMS BEGIN DIALOG %IDD_dlgPBLiteBrowser->->
  LOCAL hDlg  AS DWORD

  DIALOG NEW PIXELS, hParent, "PB Lite Browser", 229, 201, 850, 550, %WS_POPUP OR _
    %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
    %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR _
    %WS_THICKFRAME OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
    %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
    %WS_EX_RIGHTSCROLLBAR, TO hDlg
  CONTROL ADD STATUSBAR, hDlg, %IDC_STATUSBAR1, "", 0, 0, 0, 0
  CONTROL ADD TEXTBOX,   hDlg, %IDC_txtURL, "", 135, 5, 475, 20
  CONTROL ADD IMGBUTTON, hDlg, %IDC_IMGback, "", 0, 0, 32, 32
  CONTROL ADD IMGBUTTON, hDlg, %IDC_IMGForward, "", 33, 0, 32, 32
  CONTROL ADD IMGBUTTON, hDlg, %IDC_IMGReload, "", 66, 0, 32, 32
  CONTROL ADD IMGBUTTONX, hDlg, %IDC_IMGHome, "", 99, 0, 32, 32
  CONTROL ADD TAB,       hDlg, %IDC_TAB1, "Tab1", 0, 35, 610, 20
#PBFORMS END DIALOG
  lngWide = 835
  lngHigh = 450
  CONTROL ADD GRAPHIC, hDlg, %IDC_Graphic, "", 5, 56, lngWide, lngHigh, _
                       %SS_NOTIFY OR %SS_SUNKEN
                       '
  ' attach to the graphics control
  GRAPHIC ATTACH hDlg,%IDC_Graphic , REDRAW
  ' clear the control and fill it with solid white colour
  GRAPHIC CLEAR %RGB_WHITE ,0
  '
  ' turn on the graphics wrap
  GRAPHIC SET WORDWRAP %TRUE
  '
  ' set the size of a virtual graphics control
  GRAPHIC SET VIRTUAL  lngWide * %WidthMultiplier, _
                       lngHigh * %HeightMultiplier

  ' redraw the screen for the user
  GRAPHIC REDRAW
  '
  ' set the images on controls
  PREFIX "CONTROL SET IMGBUTTON hDlg,"
    %IDC_IMGback,   "#2001"
    %IDC_IMGForward,"#2002"
    %IDC_IMGReload, "#2003"
  END PREFIX
  ' load the Home button icon
  CONTROL SET IMGBUTTONX hDlg,%IDC_IMGHome,"#2000"
  '
  ' set the font on the URL text box
  CONTROL SET FONT hDlg,%IDC_txtURL, g_hFont
  '
  ' set dialog to accept drag and drop
  DragAcceptFiles hDlg, %TRUE
  '
  ' set the icon for the dialog
  DIALOG SET ICON hDlg, "#2000"
  '
  ' store the global handle for this dialog
  g_hDlg = hDlg
  '
  DIALOG SHOW MODAL hDlg, CALL ShowdlgPBLiteBrowserProc TO lngResult
  '
  FUNCTION = lngResult
  '
END FUNCTION
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
CALLBACK FUNCTION ShowdlgTabClickProc()
  LOCAL lngPageNumber AS LONG
  LOCAL hParent AS LONG
  LOCAL lngTabSelected AS LONG
  '
  SELECT CASE AS LONG CB.MSG
    CASE %WM_INITDIALOG
    ' Initialization handler

    CASE %WM_NCACTIVATE
      STATIC hWndSaveFocus AS DWORD
      IF ISFALSE CB.WPARAM THEN
        ' Save control focus
        hWndSaveFocus = GetFocus()
      ELSEIF hWndSaveFocus THEN
        ' Restore control focus
        SetFocus(hWndSaveFocus)
        hWndSaveFocus = 0
      END IF

    CASE %WM_COMMAND
      ' Process control notifications
      SELECT CASE AS LONG CB.CTL
        CASE %IDC_btnCloseTab
          IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
          ' close the current tab
            IF g_lngTabSelected <= %MAX_TABS THEN
            ' ensure tab is not beyond last element of array
              TAB DELETE g_hDlg, %IDC_TAB1, g_lngTabSelected
              '
              ' remove details of tab history
              funRemoveTabHistory(g_lngTabSelected)
              '
              ' reduce current selected tab number by 1
              DECR g_lngTabSelected
              '
                ' reset to new focused tab
              TAB SELECT g_hDlg, %IDC_TAB1, g_lngTabSelected
              CONTROL SET TEXT g_hDlg,%IDC_txtURL, _
                               g_astrURLS(g_lngTabSelected)
                 ' clear and rerender the web page
              funClearGraphicsControl()
              '
              funRenderTheHTML(g_astrURLS(g_lngTabSelected), _
                               g_hDlg, %IDC_TAB1)
            END IF
            DIALOG END CB.HNDL
          END IF
          '
      END SELECT
  END SELECT
END FUNCTION
'------------------------------------------------------------------------------
FUNCTION funRemoveTabHistory(lngTabSelected AS LONG) AS LONG
' tab has been deleted so remove it's history
  LOCAL lngR AS LONG
  LOCAL lngT AS LONG
  '
  ' first delete the current URL from the array
  ' and move all tabs URLs above it down by one
  ARRAY DELETE g_astrURLS(lngTabSelected)
  '
  ' now purge the history
  ARRAY DELETE g_lngLatestSlot(lngTabSelected)
  '
  FOR lngT = lngTabSelected TO %MAX_TABS -1
    FOR lngR = 1 TO %MAX_HISTORY
    ' copy tab above down
      g_astrURLhistory(lngT,lngR) = _
                       g_astrURLhistory(lngT +1,lngR)
    NEXT lngR
  NEXT lngT
  '
  ' now purge last tab
  FOR lngR = 1 TO %MAX_HISTORY
   g_astrURLhistory(%MAX_TABS,lngR) = ""
  NEXT lngR
  '
END FUNCTION
'------------------------------------------------------------------------------
FUNCTION ShowdlgTabClick(BYVAL hParent AS DWORD, _
                         lngX AS LONG, lngY AS LONG) AS LONG
  LOCAL lRslt AS LONG
  LOCAL lngPX , lngPY AS LONG ' parent dialog location
  '
#PBFORMS BEGIN DIALOG %IDD_dlgTabClick->->
  LOCAL hDlg  AS DWORD

  DIALOG NEW hParent, "", 268, 273, 80, 20, %WS_POPUP OR %WS_CLIPSIBLINGS OR _
    %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
    %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
    %WS_EX_RIGHTSCROLLBAR, TO hDlg
  CONTROL ADD BUTTON, hDlg, %IDC_btnCloseTab, "Close Tab", 0, 0, 80, 15
#PBFORMS END DIALOG
  '
  ' get location of parent
  DIALOG GET LOC hParent TO lngPX, lngPY
  ' set this dialogs location
  DIALOG SET LOC hDlg, lngPX,lngPY
  '
  DIALOG SHOW MODAL hDlg, CALL ShowdlgTabClickProc TO lRslt
  '
#PBFORMS BEGIN CLEANUP %IDD_dlgTabClick
#PBFORMS END CLEANUP
  '
  FUNCTION = lRslt
END FUNCTION
'------------------------------------------------------------------------------